package com.beiding.delaytask.listener;

import com.alibaba.fastjson.JSON;
import com.beiding.delaytask.entity.TimeTask;
import com.beiding.delaytask.fram.TimeControl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 丁常磊
 * @date 2021/8/12 15:33
 */
@Component
@Slf4j
public class TriggerListener {

    @Autowired
    private TimeControl timeControl;

    /**
     * 延时队列触发任务
     */
    @RabbitListener(queues = {
            "${delay-task.namespace:delay-task}.queue.trigger"
    }, containerFactory = "delayTaskCustomerFactory")
    public void trigger(String text) {
        TimeTask timeTask;
        try {
            timeTask = JSON.parseObject(text, TimeTask.class);
        } catch (Exception e) {
            log.error("无法解析的定时任务 text={}",text);
            e.printStackTrace();
            return;
        }
        timeControl.trigger(timeTask);
    }

}
